package cn.hrlly.tao2shou.callback;

import com.google.gson.Gson;
import com.zhy.http.okhttp.callback.Callback;

import cn.hrlly.tao2shou.R;
import cn.hrlly.tao2shou.domain.Commodity;
import cn.hrlly.tao2shou.listener.OnCommodityDetailsListener;
import okhttp3.Request;
import okhttp3.Response;

/**
 * User: gengliang
 * Date: 2016-01-28
 * Time: 19:16
 */

public class CommodityDetailsCallback extends Callback<Commodity> {

    private OnCommodityDetailsListener onCommodityDetailsListener;

    public CommodityDetailsCallback(OnCommodityDetailsListener onCommodityDetailsListener){
        this.onCommodityDetailsListener=onCommodityDetailsListener;
    }

    @Override
    public Commodity parseNetworkResponse(Response response) throws Exception {
        String string=response.body().string();
        Commodity commodity=new Gson().fromJson(string,Commodity.class);
        return commodity;
    }

    @Override
    public void onError(Request request, Exception e) {
        onCommodityDetailsListener.showCommodityDetailsFail(R.string.network_connection_error);
    }

    @Override
    public void onResponse(Commodity commodity) {
        if(commodity.getStatus()==0)
            onCommodityDetailsListener.showCommodityDetailsSucc(commodity);
        else
            onCommodityDetailsListener.showCommodityDetailsFail(commodity.getErrorMessage());
    }
}
